﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraBars;
using Sunboxsoft.WinForm;
using DevExpress.Utils;

namespace Sunboxsoft.WinForm.Shell.Utils
{
    class ToolbarUtil
    {
        public static void ApplyToToolbar(Bar bar, ActionNodeCollection actions)
        {
            ImageCollection images = new ImageCollection(true);
            images.ImageSize = new System.Drawing.Size(16, 16);
            ImageCollection largeImages = new ImageCollection(true);
            largeImages.ImageSize = new System.Drawing.Size(32, 32);

            bar.Manager.Items.Clear();
            bar.ItemLinks.Clear();
            bar.Manager.Images = images;
            bar.Manager.LargeImages = largeImages;


            for (int index = 0; index < actions.Count; index++)
            {
                ActionNode node = actions[index];

                BarButtonItem button = new BarButtonItem();
                button.Name = node.Name;
                button.PaintStyle = BarItemPaintStyle.CaptionGlyph;
                button.Caption = node.Caption;

                if (!node.IsShow)
                {
                    button.Visibility = BarItemVisibility.Never;
                }

                if (!node.IsEnable)
                {
                    button.Enabled = false;
                }

                if (node.ClickCallback != null)
                {
                    button.ItemClick += new ItemClickEventHandler(node.ClickCallback);
                }

                if (node.Image != null)
                {
                    images.AddImage(node.Image);
                    button.ImageIndex = images.Images.Count - 1;
                }

                if (node.LargeImage != null)
                {
                    largeImages.AddImage(node.LargeImage);
                    button.LargeImageIndex = largeImages.Images.Count - 1;
                }

                bar.Manager.Items.Add(button);
                bar.ItemLinks.Add(button);
            }
        }
    }
}
